package datamining.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import settings.Settings;

import datamining.algorithms.prefixspan.PrefixSpanSequenceFactory;
import datamining.miner.general.Fragment;
import datamining.parsers.DatSeqParser;
import datamining.sequence.Sequence;
import datamining.sequence.SequenceFactory;

/**
 * @author xujinghu, E-mail: jinghuxu@nudt.edu.cn
 * @date 2012-11-14 下午2:22:14
 * @version
 */
public class Miner {

	/**
	 * @author xujinghu
	 * @param sequences
	 * @param settings
	 * @return 
	 */
	public static <ItemType> Collection<Fragment<ItemType>> mine(
			final List<Sequence<ItemType>> sequences,
			final SequenceFactory<ItemType> factory,
			final Settings settings) {
		System.out.println(settings);		//打印配置参数
		
		settings.algorithm.initialize(sequences, factory, settings);		//初始化算法数据	
		
		Collection<Fragment<ItemType>> ret = settings.strategy.search(settings.algorithm);	//挖掘频繁序列模式
		
		return ret;
	}

	/**
	 * 转换输入数据到内部序列表示
	 * 
	 * @author xujinghu
	 * @param settings
	 * @return 
	 */
	public static final <ItemType> ArrayList<Sequence<ItemType>> parseInput(
			final Settings<ItemType> settings) {
		String dataFile = settings.dataFile;
		ArrayList<Sequence<ItemType>> transaction = new ArrayList<Sequence<ItemType>>();

		try {
			File file = new File(dataFile);
			if (! file.exists() && ! file.isDirectory()) {
				file.createNewFile();
			} else {
				DatSeqParser<ItemType> datSeqParser = new DatSeqParser<ItemType>();
				transaction = (ArrayList<Sequence<ItemType>>) datSeqParser.parse(new FileReader(file), new PrefixSpanSequenceFactory<ItemType>());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return transaction;
	}
	
}
